<?php
/**
 * 2016/10/23 10:24:51
 * 张树超
 * 用户模型
 */
namespace Home\Model;
use Think\Model;

class UserModel extends Model{
	protected $tableName = 'user'; // 数据库
	
	/**
	 * 自动验证
	 */
	protected $_validate = array(
		array('username','','帐号名称已经存在！',1,'unique',1),
		array('email','','邮箱已经存在！',0,'unique',3),
		array('mobile','','手机已经存在！',1,'unique',3),

		array('email','email','邮箱格式错误！',0),
		array('mobile','/^1[3|4|5|6|7|8]\d{9}/','手机格式错误',0,'regex'),
		array('password','require','密码不能为空！',0),
		array('password','3,20','密码至少3位！',0,'length'),
		array('repassword','password','确认密码不正确',0,'confirm'),
	);
	
	/**
	 * 登录
	 */
	public function login() {
		$username = I('post.username','','strip_tags');
		$password = I('post.password','','strip_tags,md5');
		if(!$username){
			return false;
		}
		$map['_string'] = "(mobile='{$username}' OR username='{$username}' OR email='{$username}') AND password='{$password}'";

		$info = $this->where($map)->find();

        hook("login_model",$info);
		return $info;
	}

	/**
	 * 注册
	 */
	public function register() {
		$data['username'] = I('post.username','','strip_tags');
		$data['email'] = I('post.email','','strip_tags');
		$data['password'] = I('post.password','','trim,strip_tags,md5');
		$data['mobile'] = I('post.mobile','');
		$data['status'] = 1;
		$msg = $this->add($data);
		return $msg;
	}

	/**
	 * 修改用户信息
	 */
	public function profile() {
		$data['email'] = I('post.email','','strip_tags');
		$data['mobile'] = I('post.mobile','','intval');
		$msg = $this->where(array('id'=>intval($_SESSION['member']['id'])))->save($data);
		return $msg;
	}

	/**
	 * 修改密码
	 */
	public function password() {
		$data['password'] = I('post.password','','trim,strip_tags,md5');
		$msg = $this->where(array('id'=>intval($_SESSION['member']['id'])))->save($data);
		return $msg;
	}
}
